import 'package:app/util/StringUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import './FormItem.dart';
import '../input/index.dart';
import '../button/index.dart';
import '../checkbox/index.dart';
import '../input/input.dart';
import '../radio-group/index.dart';


class Forms extends Form {

  Forms({
    required List<FormItem> formItem,
    GlobalKey<FormState>? formKey
  }) : super(
            key: formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: getChildren(formItem),
            ));

  static Future<bool> validate(GlobalKey<FormState> formKey) async{
     FormState form = formKey.currentState as FormState;
     return form.validate();
  }

  static List<Widget> getChildren(List<FormItem> list){
    return list.map((e) => Padding(
      padding: EdgeInsets.only(top: e.rowSpacing ?? 10),
      child: getComponents(e)
    )).toList();
  }

  static Widget getComponents(FormItem item){
    TextEditingController controller = TextEditingController(text: item.value);
    controller.selection = TextSelection.fromPosition(TextPosition(affinity: TextAffinity.downstream, offset: controller.text.length));
    switch(item.type){
      case 'inputText' || 'password':
        return InputText(
          onEntry: item.onEntry,
          focusNode: item.focusNode,
          controller: controller,
          labelText: item.labelText,
          prefixIcon: item.icon,
          obscureText: item.type == 'password' ? true : false,
          validator: (value) {
            return validateFlied(item, value!);
          },
          onChanged: item.onChange,
        );
      case 'button':
        return Button(
            label: item.labelText,
            onPressed: item.onClick,
            height: item.height ?? 50,
            fontSize: item.fontSize ?? 14,
        );
      case 'checkbox':
        return CheckBox(
            label: item.labelText,
            checked: item.checked,
            onChanged: item.onChange,
        );
      case 'input' || 'inputPassword':
        return Input(
          onEntry: item.onEntry,
          focusNode: item.focusNode,
          controller: controller,
          label: item.labelText,
          suffixIcon: item.icon,
          placeholder: item.placeholder,
          onClick: item.onClick,
          enabled: item.enabled,
          initialValue: item.initialValue,
          readOnly: item.readOnly,
          labelWidth: item.labelWidth,
          labelAlignment: item.labelAlignment,
          obscureText: item.type == 'inputPassword' ? true : false,
          validator: (value) {
            return validateFlied(item, value!);
          },
          onTap: item.iconOnTap,
          onChanged: item.onChange,
        );
      case 'radioGroup':
        return RadioGroup(
            list: item.radioItem!,
            labelWidth: item.labelWidth,
            label: item.labelText,
            labelAlignment: item.labelAlignment,
            fontSize: item.fontSize,
            context: item.context,
        );
      default: throw Exception("component type error");
    }
  }

  static dynamic validateFlied(FormItem item, String value){
    String message = "";
    if(StringUtils.isNotNull(item.rules) && item.validate!){
      for(var key in item.rules!.keys){
        List<dynamic>? list = item.rules![key];
        message = check(list, item.value??'');
      }
    }
    return StringUtils.isNotNull(message) ? message : null;
  }

  static String check(List<dynamic>? rules, String value){
    return rules!.map((item) => item['required'] && value.isEmpty ? item['message'] : "").
      toString().trim().replaceAll(RegExp(r'^\(|\)$'), '');
  }
}
